﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace LogAnalytics.Tools
{
    public partial class Option : Form
    {
        public Option()
        {
            InitializeComponent();
            loadStock();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            this.gbtnApply.Enabled = true;
            return base.ProcessCmdKey(ref msg, keyData);
        }

        #region Event

        #region TabControl

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (((TabControl)sender).SelectedTab.Name)
            {
                case "tpStock":
                    loadStock();
                    break;
                case "tpDataBase":
                    loadDataBase();
                    break;
                default:
                    break;
            }
        }

        #region tabPage1

        private void pbOpenFile_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog myFolderDialog = new FolderBrowserDialog();
            if (myFolderDialog.ShowDialog() == DialogResult.Cancel) return;
            this.tbLogPath.Text = myFolderDialog.SelectedPath;
            this.gbtnApply.Enabled = true;
        }

        #endregion

        #endregion

        #region GlassButton

        private void gbtnApply_Click(object sender, EventArgs e)
        {
            try
            {
                switch (this.tabControl1.SelectedTab.Name)
                {
                    case "tpStock":
                        if (!saveStock()) return;
                        break;
                    case "tpDataBase":
                        if(!saveDataBase()) return;
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            this.gbtnApply.Enabled = false;
        }

        private void gbtnOK_Click(object sender, EventArgs e)
        {
            gbtnApply_Click(sender, e);
            this.Close();
        }

        private void gbtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region ComboBox

        private void cbbPageExclude_CheckedChanged(object sender, EventArgs e)
        {
            this.gbtnApply.Enabled = true;
        }

        #endregion

        #region RadioButton

        private void rbtnTop_CheckedChanged(object sender, EventArgs e)
        {
            this.tbTop.Enabled = this.rbtnTop.Checked;
        }

        private void rbtnPercent_CheckedChanged(object sender, EventArgs e)
        {
            this.tbPercent.Enabled = this.rbtnPercent.Checked;
        }

        #endregion

        #endregion

        #region Method

        private void loadStock()
        {
            if (this.tpStock.Tag != null) return;
            string pageexclude = ConfigurationManager.AppSettings["pageexclude"];
            this.cbbPageExclude.Checked = pageexclude == "1" ? true : false;
            string savetype = ConfigurationManager.AppSettings["SaveType"];
            switch (savetype)
            {
                case "3":
                    this.rbtnPercent.Checked = true;
                    this.tbPercent.Text = ConfigurationManager.AppSettings["SaveValue"];
                    break;
                case "2":
                    this.rbtnTop.Checked = true;
                    this.tbTop.Text = ConfigurationManager.AppSettings["SaveValue"];
                    break;
                default:
                    this.rbtnAll.Checked = true;
                    break;
            }
            this.tbLogPath.Text = ConfigurationManager.AppSettings["logs"];
            this.tpStock.Tag = "1";
        }

        private bool saveStock()
        {
            Regex regex=new Regex(@"^\d+$");

            if (this.rbtnTop.Checked&&(!regex.IsMatch(this.tbTop.Text)))
            {
                MessageBox.Show("指定数量的数值格式不正确");
                return false;
            }
            if (this.rbtnPercent.Checked && (!regex.IsMatch(this.tbPercent.Text)))
            {
                MessageBox.Show("百分比的数值格式不正确");
                return false;
            }

            Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            appConfig.AppSettings.Settings["pageexclude"].Value = this.cbbPageExclude.Checked ? "1" : "0";
            if (this.rbtnAll.Checked) appConfig.AppSettings.Settings["SaveType"].Value = "1";
            if (this.rbtnTop.Checked)
            {
                appConfig.AppSettings.Settings["SaveType"].Value = "2";
                appConfig.AppSettings.Settings["SaveValue"].Value = this.tbTop.Text;
            }
            if(this.rbtnPercent.Checked)
            {
                appConfig.AppSettings.Settings["SaveType"].Value = "3";
                appConfig.AppSettings.Settings["SaveValue"].Value = this.tbPercent.Text;
            }
            appConfig.AppSettings.Settings["logs"].Value = this.tbLogPath.Text;
            appConfig.Save();
            appConfig = null;
            ConfigurationManager.RefreshSection("appSettings");

            return true;
        }

        private void loadDataBase()
        {
            if (this.tpDataBase.Tag != null) return;
            string Server = ConfigurationManager.AppSettings["Server"];
            if (!string.IsNullOrEmpty(Server)) this.tbServer.Text = Server;
            string DataBase = ConfigurationManager.AppSettings["DataBase"];
            if (!string.IsNullOrEmpty(DataBase)) this.tbDataBase.Text = DataBase;
            string Userid = ConfigurationManager.AppSettings["UserID"];
            if (!string.IsNullOrEmpty(Userid)) this.tbUserID.Text = QT.Security.AESHelper.AESDecrypt(Userid);
            this.tpDataBase.Tag = "1";
        }

        private bool saveDataBase()
        {
            if (this.tbServer.Text.Length == 0)
            {
                MessageBox.Show("请设置数据库服务器地址");
                return false; 
            }
            if (this.tbDataBase.Text.Length == 0)
            {
                MessageBox.Show("请设置数据库名称");
                return false;
            }
            if (this.tbPassword.Text.Length > 0 && this.tbUserID.Text.Length == 0)
            {
                MessageBox.Show("请设置数据库账号");
                return false;
            }

            Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            appConfig.AppSettings.Settings["Server"].Value = this.tbServer.Text;
            appConfig.AppSettings.Settings["DataBase"].Value = this.tbDataBase.Text;
            if (this.tbPassword.Text.Length > 0)
            {
                appConfig.AppSettings.Settings["UserID"].Value = QT.Security.AESHelper.AESEncrypt(this.tbUserID.Text);
                appConfig.AppSettings.Settings["Password"].Value = QT.Security.AESHelper.AESEncrypt(this.tbPassword.Text);
            }
            appConfig.Save();
            appConfig = null;
            ConfigurationManager.RefreshSection("appSettings");

            return true;
        }

        #endregion
    }
}
